﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace VAdvantage.Grids
{
    public class VDataGridCheckBoxColumn : DataGridCheckBoxColumn, IDataGridColumn
    {
        CheckBox chk = null;
        GridField _field;
        private string _columnName = "";


        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (chk != null)
            {
                _field.SetValue(chk.IsChecked == null? "false" : ((bool)chk.IsChecked).ToString());
                chk = null;
            }
            return base.GenerateElement(cell, dataItem);
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            return base.GenerateEditingElement(cell, dataItem);
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            object control = base.PrepareCellForEdit(editingElement, editingEventArgs);

          

            chk = editingElement as CheckBox;
           
            DataGridView.RaiseCellValueChanged(chk);
            return control;

        }


        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }

            set
            {
                _columnName = value.ToUpper();
                this.Binding = new System.Windows.Data.Binding(_columnName);
            }
        }

        public Controls.VDataGrid DataGridView
        {
            get;
            set;
        }

        public void Dispose()
        {
            _field = null;
            chk = null;
        }


        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
            }
        }

        public override System.Windows.Data.Binding Binding
        {
            get
            {
                return base.Binding;
            }
            set
            {
                base.Binding = value;
            }
        
        }

        public bool ReadOnly
        {

            get;
            set;

        }

    }
}
